package cfca.paperless.client.servlet._1_basic._1_06_compoundSignatureAutoPdfWithPath;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.FileUtils;

import cfca.paperless.client.bean.ChannelBean;
import cfca.paperless.client.bean.MultiDataBean;
import cfca.paperless.client.bean.SealUserInfo;
import cfca.paperless.client.bean.SignLocation;
import cfca.paperless.client.servlet.PaperlessClient;
import cfca.paperless.client.servlet.PaperlessConfig;
import cfca.paperless.client.util.GUID;
import cfca.paperless.client.util.ImageUtil;
import cfca.paperless.client.util.IoUtil;
import cfca.paperless.client.util.PaperlessUtil;
import cfca.paperless.client.util.StrategyUtil;
import cfca.paperless.client.util.StringUtil;

public class CompoundSignatureAutoPdfWithPathTest01 {

    public static void main(String[] args) throws Exception {

        try {
            PaperlessClient clientBean = new PaperlessClient(PaperlessConfig.url, 10000, 60000);
            
///////////////////////////如下为用户1手写签名相关的数据，如果没用用户1此处可以省略/////////////////////////////////////////////////
            String pngFilePath = "./TestData/handwriting.png";
            
            String pngSuffix = IoUtil.getFileNameSuffix(pngFilePath);
            if(!PaperlessConfig.PNG_FILE_SUFFIX.equals(pngSuffix)){
                throw new Exception("所选文件不是png文件,签章图片文件只支持png格式图片");
            }
            byte[] handwritingImage1 = FileUtils.readFileToByteArray(new File(pngFilePath));
            ImageUtil.resizeImage(handwritingImage1, 20, 20);
            byte[] handwritingData1 = handwritingImage1;
            
            pngFilePath= "./TestData/1.png";
            pngSuffix = IoUtil.getFileNameSuffix(pngFilePath);
            if(!PaperlessConfig.PNG_FILE_SUFFIX.equals(pngSuffix)){
                throw new Exception("所选文件不是png文件,签章图片文件只支持png格式图片");
            }
            byte[] identificationFrontData = FileUtils.readFileToByteArray(new File(pngFilePath));
            
            pngFilePath= "./TestData/2.png";
            pngSuffix = IoUtil.getFileNameSuffix(pngFilePath);
            if(!PaperlessConfig.PNG_FILE_SUFFIX.equals(pngSuffix)){
                throw new Exception("所选文件不是png文件,签章图片文件只支持png格式图片");
            }
            byte[] identificationBackData = FileUtils.readFileToByteArray(new File(pngFilePath));
 
            List<MultiDataBean> list1 = new ArrayList<MultiDataBean>();
            list1.add(new MultiDataBean(handwritingData1, "handwritingData.dat", "media-id", "2"));//手写签名数据
            list1.add(new MultiDataBean(handwritingImage1, "handwritingImage.png", "2"));//手写签名图片
            list1.add(new MultiDataBean(identificationFrontData, "identificationFrontImage.png", "2"));//身份证正面
            list1.add(new MultiDataBean(identificationBackData, "identificationBackImage.png", "2"));//身份证反面
 
            String[] proofHashAndMultiData1 = PaperlessUtil.getProofHashAndMultiData(list1);

            SignLocation signLocation = new SignLocation(1,  50, 50);
            signLocation.setKeywordPositionIndex("2,2,4,9");
            String userName="张小虎";//256
            String idType ="01";//2
            String idNo ="123456789012345678";//80
            
            SealUserInfo sealUserInfo = new SealUserInfo(userName, idType, idNo, "157446598231", "北京", "签合同");
 
            
            // 设置印章显示大小，可以不设置。比如：100，代表100px。比如需要把章显示为4cm（圆章且DPI为96）时，传入151即可。（并且imageScale配置为0.87）
            //sealUserInfo.setSealImageSize(100);
            
            String proofSealStrategyXml1 = StrategyUtil.createProofSealStrategyXml(handwritingImage1, sealUserInfo, signLocation, proofHashAndMultiData1[0]);
            String sealLayer2Text = "张三";
            String sealLayer2TextStyle = "";//"font-size:12;";
            //String proofSealStrategyXml1 = StrategyUtil.createProofSealStrategyXml(sealLayer2Text,sealLayer2TextStyle, sealUserInfo, signLocation, proofHashAndMultiData1[0]);
            System.out.println(proofSealStrategyXml1);
///////////////////////////以上为用户1的数据/////////////////////////////////////////////////
            ///////////////////////////如下为加盖机构印章的策略/////////////////////////////////////////////////
            signLocation = new SignLocation("银行签章", "D", 20, 20);
//            signLocation.setPage(1);
            sealUserInfo = new SealUserInfo("签章者", "北京", "签合同");
            sealUserInfo.setBusinessCode("0000000");
             
            String sealCode = "yz2018051448122040";
            String sealPassword = "cfca1234";
            String sealStrategyXml = StrategyUtil.createSealStrategyXml(sealCode, sealPassword, sealUserInfo, signLocation);

///////////////////////////以上为加盖机构印章的策略//////////////////////////////////////////////
        
            String compoundSealStrategyXml = "<List>" + proofSealStrategyXml1 + sealStrategyXml +   "</List>";
           
            String multiData = "[" + proofHashAndMultiData1[1] + "]";
           
            String pdfString = "D:/TestData/file.pdf";
            
            long timeStart = System.currentTimeMillis();
            String channelBeanXml = new ChannelBean(PaperlessConfig.channelCode).toString();
            
            // 获取场景证书的方式，与证据证书签章策略文件配合使用。默认值为0。0：实时从CFCA CA服务申请下载场景证书；1：使用从CFCA CA服务预先申请下载并存储在本地的场景证书；
            String sceneCertChannel = "0";
            
            // 获取时间戳的方式。默认值为0。0：实时访问CFCA 时间戳服务；1：使用从CFCA购置并在本地部署的时间戳服务器产品；
            String timestampChannel = "0";
            

            String systemNo = GUID.generateId();
            
            String outputFilePath = "D:/TestData/output/compoundSignatureAutoPdfWithPathTest01.pdf";
            
            String result = clientBean.compoundSignatureAutoPdfWithPath(systemNo,pdfString, outputFilePath,multiData, compoundSealStrategyXml,
                    PaperlessConfig.operatorCode, channelBeanXml, sceneCertChannel, timestampChannel);
           
            String code = StringUtil.getNodeText(result, "Code");
            // 返回值Code等于200代表后台处理成功
            if (code.equals("200")) {
                System.out.println("CompoundSignatureAutoPdfWithPathTest01 END OK");
                outputFilePath = StringUtil.getNodeText(result, "OutputFilePath");
                System.out.println("outputFilePath:" + outputFilePath);
            }else {
                // 处理结果为异常，打印出错误码和错误信息
                System.out.println("CompoundSignatureAutoPdfWithPathTest01 END NG");
                System.out.println(result);
            }
     
            long timeEnd = System.currentTimeMillis();// 结束时间
            System.out.println("########## CompoundSignatureAutoPdfWithPathTest01 time used:" + (timeEnd - timeStart) + "##########");
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
